// pages/usermodify/usermodify.js

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    usersex: '',
    userInfo: '',
    actions: [{
        name: '男',
        value: 1
      },
      {
        name: '女',
        value: 2
      },
      // {
      //   name: '保密',
      //   subname: '不建议您这样选择',
      //   openType: 'contact',
      // },
    ],
  },
  navTo(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
  sex() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onSelect(event) {
    console.log(event.detail.value);
    this.data.userInfo.gender = event.detail.value
    this.setData({
      userInfo: this.data.userInfo
    })
  },
  formSubmit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },
  onChooseAvatar(e) {
    let that = this;
    const { avatarUrl } = e.detail 
    wx.uploadFile({
      url: app.globalData.apiRoot + '/storage/signature', //仅为示例，非真实的接口地址
      filePath: avatarUrl,
      name: 'file',
      formData: {},
      header: {
        "Authorization": 'Bearer ' + wx.getStorageSync('access')
      },
      success (res){
        const data = JSON.parse(res.data)
        that.data.userInfo.avatarUrl = app.globalData.imageRoot + data.data.fileName
        that.setData({
          userInfo: that.data.userInfo
        })
      }
    })
  
  },
  chooseImg() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths

        wx.uploadFile({
          url: app.globalData.apiRoot + '/storage/signature', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {},
          header: {
            "Authorization": 'Bearer ' + wx.getStorageSync('access')
          },
          success (res){
            const data = JSON.parse(res.data)
            that.data.userInfo.avatarUrl = app.globalData.imageRoot + data.data.fileName
            that.setData({
              userInfo: that.data.userInfo
            })
          }
        })
        
        // that.data.userInfo.avatarUrl = tempFilePaths[0]
        // that.setData({
        //   userInfo: that.data.userInfo
        // })
      }
    })
  },
  submit() {
    let that = this;
    let data = this.data.userInfo
    wx.showModal({
      title: '提示',
      content: '是否确认修改信息',
      success(res) {
        if (res.confirm) {
          data.ext1 = 1;
          getApp().ports.modifyInfo({
            data
          }).then((res) => {
            wx.showToast({
              title: '修改成功',
            })
          }).catch(() => {
            console.error('出啥错了？');
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
   
  },

  nameInput(e) {
    this.data.userInfo.nickname = e.detail.value
    this.setData({
      userInfo: this.data.userInfo
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    getApp().ports.getInfo({}).then((res) => {
      let userInfo = {
        avatarUrl: res.data.avatarUrl,
        phone: res.data.phone,
        nickname: res.data.nickname,
        gender: res.data.gender,
        // information: {
        //   gender: res.data.information.gender
        // }
      }
      that.setData({
        userInfo: userInfo
      })
    }).catch(() => {
      console.error("获取自身信息出错！")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})